package com.the4man.cnbdw.listener.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Arrays;

/**
 * Created by lenovo on 2017/12/12.
 */
public class SocketOperate extends Thread {
    private Socket socket;
    private byte[] left;

    public SocketOperate(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            InputStream in = this.socket.getInputStream();
            PrintWriter out = new PrintWriter(this.socket.getOutputStream());
            System.out.println("run()");

            String res;
            do {
                byte[] recv = new byte[1024];
//                int length = false;
                int length = in.read(recv, 0, recv.length);
                System.out.println("length:" + length);
                if(length == -1) {
                    System.out.println("length:-1");
                    return;
                }

//                byte[] bs = null;
                if(this.left != null && this.left.length > 0) {
                    Arrays.copyOf(this.left, length);
                }

                byte[] bs = Arrays.copyOf(recv, length);
                res = byte2HexStr(bs);
                System.out.println("recv:" + res);
//                byte[] tmp = null;
                byte[] tmp = Arrays.copyOf(bs, bs.length);
                String apMac = null;
                String eqMac = null;

                for(int i = 0; i < tmp.length; ++i) {
                    if(tmp.length - i < 96) {
                        this.left = new byte[tmp.length - i];

                        for(int j = 0; j < this.left.length; ++j) {
                            this.left[j] = tmp[i + j];
                        }

                        System.out.println("left bytes:" + byte2HexStr(this.left));
                        break;
                    }

                    if(tmp[i] == 12 && tmp[i + 1] == 12 && tmp[i + 2] == 8 && tmp[i + 8] == 13 && tmp[i + 9] == 6) {
                        byte[] apMac_bs = new byte[12];
                        byte[] eqMac_bs = new byte[12];

                        for(int j = 0; j < 12; ++j) {
                            apMac_bs[j] = tmp[i + 16 + j];
                            eqMac_bs[j] = tmp[i + 32 + j];
                        }

                        apMac = byte2HexStr(apMac_bs);
                        eqMac = byte2HexStr(eqMac_bs);
                        System.out.println("apmac:" + apMac);
                        System.out.println("eqmac:" + eqMac);
                        i += 95;
                    }
                }
            } while(!"".equals(res) && res != null);

            out.flush();
            out.close();
        } catch (Exception var51) {
            var51.printStackTrace();
            System.out.println("exception");
        } finally {
            try {
                this.socket.close();
            } catch (IOException var49) {
                var49.printStackTrace();
                System.out.println("socket stop error.....");
            } finally {
                this.socket = null;
            }

            System.out.println("socket stop.....");
        }

    }

    public static byte[] toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];

        for(int i = 0; i < baKeyword.length; ++i) {
            try {
                baKeyword[i] = (byte)(255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return baKeyword;
    }

    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    public static byte[] hexStr2Bytes(String src) {
//        int m = false;
//        int n = false;
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }

        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte)(b0 | b1);
        return ret;
    }
}
